var app = new Vue({
    el: '#confirmOrder',
    data () {
        return {
            payWay: '微信支付',
            options: [
                {
                    label: '微信支付',
                    value: '微信支付',
                    src: '../../image/icon-WeChat20190518.png'
                },
                {
                    label: '支付宝支付',
                    value: '支付宝支付',
                    src: '../../image/icon-Alipay20190518.png'
                },
                {
                    label: '银联支付',
                    value: '银联支付',
                    src: '../../image/icon-UnionPay20190518.png'
                },
                {
                    label: '现金支付',
                    value: '现金支付',
                    src: '../../image/icon-money20190518.png'
                }
            ],
            buyerMessage: '',
            addressData: null,
            orderData: null,
            isDisabled: false
        }
    },
    created() {
        this.getAddress()
        let UrlParams = getUrlParams()
        if (Object.keys(UrlParams).length > 0) {
            this.getBuyData()
        } else {
            this.getOrderData()
        }
    },
    methods: {
        // 返回上一页
        goback () {
            window.history.go(-1)
        },
        // 支付方式弹窗弹出
        changePayWay () {
            const myPopup = this.$refs.myPopup
            myPopup.show()
        },
        // 支付方式弹窗关闭
        closePayWay () {
            const myPopup = this.$refs.myPopup
            myPopup.hide()
        },
        // 获取默认收货地址
        getAddress () {
            Axios.post('user/address/getDefaultReceiveAddress.do', {}).then(res => {
                if (res.respCode === 'S') {
                    this.addressData = res.data
                }
            })
        },
        // 从购物车进来获取订单信息
        getOrderData () {
            Axios.post('goods/shopCar/toOrder.do', {}).then(res => {
                if (res.respCode === 'S') {
                    let data = res.data
                    if (data.deliveryTime) {
                        data.deliveryTime = new Date(data.deliveryTime).Format('yy-MM-dd hh:mm:ss')
                    }
                    this.orderData = data
                }
            })
        },
        // 直接购买进来获取订单信息
        getBuyData () {
            let goodsSkuId = Number(getUrlParams().goodsSkuId)
            let quantity = Number(getUrlParams().quantity)
            Axios.post('goods/goods/goodsDirectOrder.do', {
                goodsSkuId: goodsSkuId,
                quantity: quantity
            }).then(res => {
                if (res.respCode === 'S') {
                    let data = res.data
                    if (data.deliveryTime) {
                        data.deliveryTime = new Date(data.deliveryTime).Format('yy-MM-dd hh:mm:ss')
                    }
                    this.orderData = data
                }
            })
        },
        // 点击服务费弹窗
        clickService () {
            this.$createDialog({
                type: 'alert',
                title: '服务费',
                content: '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;服务费用按照规定，不满500元，就按10元一单收费，超过500元，按照基本费用1元加上超过订单额的百分之2收费。'
            }).show()
        },
        // 点击送货时间弹窗
        clickDeliveryTime () {
            this.$createDialog({
                type: 'alert',
                title: '送达时间',
                content: '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;当订单数达到一定数量进行配送，或者每日有三个时间节点，分别为：11:00、13:00、16:00，进行配送'
            }).show()
        },
        // 提交订单
        btnSumbit () {
            this.isDisabled = true
            let UrlParams = getUrlParams()
            Axios.post('goods/order/addOrder.do', {
                receiveAddressId: this.addressData.receiveAddressId,
                saveAmount: this.orderData.offerPrice,
                serviceFee: this.orderData.serviceFee,
                extraFee: this.orderData.additionalFees,
                remark: this.buyerMessage,
                amount: this.orderData.totalPrice,
                pay: this.payWay,
                goodsSkuId: Number(UrlParams.goodsSkuId),
                quantity: Number(UrlParams.quantity)
            }).then(res => {
                if (res.respCode === 'S') {
                    this.toast = this.$createToast({
                        txt: '恭喜下单成功！您可以通过全部订单查看订单详情',
                        type: 'txt'
                    })
                    this.toast.show()
                    setTimeout(() => {
                        this.isDisabled = false
                        window.location.href = '../order/member_order.html'
                    }, 1500)
                } else {
                    if (res.errorCode === '2040') {
                        this.dialog = this.$createDialog({
                            type: 'prompt',
                            title: '温馨提示',
                            content: '您没有绑定家庭服务师，请绑定之后再来下单！',
                            prompt: {
                                value: '',
                                placeholder: '请输入家庭服务师手机号'
                            },
                            onConfirm: (e, promptValue) => {
                                if (promptValue) {
                                    Axios.post('user/user/bindingHomeHelper.do', {
                                        mobile: promptValue
                                    }).then(res => {
                                        if (res.respCode === 'S') {
                                            this.$createDialog({
                                                type: 'alert',
                                                confirmBtn: {
                                                    text: '我知道了',
                                                    active: true
                                                }
                                            }, (createElement) => {
                                                return [
                                                    createElement('div', {
                                                        'class': {
                                                        'my-title': true
                                                        },
                                                        slot: 'title'
                                                    }, [
                                                        createElement('p', '关联成功')
                                                    ]),
                                                    createElement('p', {
                                                        'class': {
                                                        'my-content': true
                                                        },
                                                        slot: 'content'
                                                    }, '成为会员可以享受更优质的服务'),
                                                    createElement('p', {
                                                        'class': {
                                                        'my-content': true
                                                        },
                                                        slot: 'content'
                                                    }, '详情请联系家庭服务师！')
                                                ]
                                            }).show()
                                            this.isDisabled = false
                                        } else {
                                            this.$createToast({
                                                type: 'warn',
                                                time: 1500,
                                                txt: res.errorMsg
                                            }).show()
                                            this.isDisabled = false
                                        }
                                    })
                                } else {
                                    this.$createToast({
                                        type: 'warn',
                                        time: 1500,
                                        txt: '请先输入家庭服务师手机号'
                                    }).show()
                                    this.isDisabled = false
                                }
                            },
                            onCancel: () => {
                                this.isDisabled = false
                            }
                        }).show()
                    } else {
                        this.toast = this.$createToast({
                            txt: res.errorMsg,
                            type: 'txt'
                        })
                        this.toast.show()
                        this.isDisabled = false
                    }
                }
            })
        }
    }
})
